const model = require('./src/db/index');
const DataType = require('sequelize');

const User = model.define('customer', {
    id: {
        type: DataType.INTEGER,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true
    },
    nickName: {
        type: DataType.STRING,
        comment: '名称',
        allowNull: false
    },
    avatarUrl: {
        type: DataType.STRING,
        comment: '头像',
        allowNull: false
    }
});

const Project = model.define('project', {
    id: {
        type: DataType.INTEGER,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true
    },
    project: {
        type: DataType.STRING,
        comment: '名称',
        allowNull: false
    }
});

const UserProject = model.define('UserProject',{
    id: {
        type: DataType.INTEGER,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true
    },
    customerId: {
        type: DataType.STRING,
        comment: '用户ID',
        allowNull: false
    },
    projectId: {
        type: DataType.INTEGER,
        comment: '项目ID',
        allowNull: false
    }
});
Project.belongsToMany(User, {as: 'userInfo', through: 'UserProject', foreignKey: 'customerId'});
User.belongsToMany(Project, {as: 'projectInfo', through: 'UserProject', foreignKey: 'projectId'});
// UserProject.sync({ force: true });
// Project.sync({ force: true });
// User.sync({ force: true });
console.log(UserProject);
User.findAll({
    include: {
        as: 'projectInfo',
        required: true,
        model: Project
    }
// eslint-disable-next-line strict
}).then( res=>{
    console.log(JSON.stringify(res));
// eslint-disable-next-line strict
}).catch( e=>{
    console.log(e);
});
